package com.jielihaofeng.atguigu.codec2;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;

/**
 * @author Johnnie Wind
 * @description Protobuf 服务端
 * @date 2021/11/8 17:36
 */
public class Protobuf2Server {

    public static void main(String[] args) throws InterruptedException {
        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(
                        new ChannelInitializer<NioSocketChannel>() {
                            @Override
                            protected void initChannel(NioSocketChannel ch) {
                                ChannelPipeline pipeline = ch.pipeline();
                                // pipeline中加入ProtobufDecoder解码器
                                pipeline.addLast("decoder", new ProtobufDecoder(MyDataInfo.MyMessage.getDefaultInstance()));
                                pipeline.addLast(new SimpleChannelInboundHandler<MyDataInfo.MyMessage>() {
                                    @Override
                                    protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.MyMessage msg) throws Exception {
                                        // 根据dataType显示不同的信息
                                        MyDataInfo.MyMessage.DataType dataType = msg.getDataType();
                                        if (dataType == MyDataInfo.MyMessage.DataType.StudentType) {
                                            MyDataInfo.Student student = msg.getStudent();
                                            System.out.println("学生id=" + student.getId() + " 学生名字=" + student.getName());
                                        } else if (dataType == MyDataInfo.MyMessage.DataType.WorkerType) {
                                            MyDataInfo.Worker worker = msg.getWorker();
                                            System.out.println("工人名字=" + worker.getName() + " 工人年龄=" + worker.getAge());
                                        } else {
                                            System.out.println("传输的类型不正确");
                                        }
                                    }
                                });
                            }
                        })
                .handler(new ChannelInitializer<NioServerSocketChannel>() {
                    @Override
                    protected void initChannel(NioServerSocketChannel nioServerSocketChannel) throws Exception {
                        System.out.println("服务器已启动，端口8088");
                    }
                })
                .bind(8088);
    }

}